/**
 * created by 小卷毛, 2019-09-23 023
 * Copyright (c) 2019, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.af.community.smart.viewmodel

import com.af.community.smart.model.CommonData
import com.af.community.smart.model.CommonModel
import com.af.community.smart.share.BaseUrl
import com.af.community.smart.utils.applyFlowableSchedulers
import com.af.community.smart.utils.applySchedulers
import com.lzg.extend.BaseResponse
import com.lzg.extend.jackson.JacksonConvert
import com.lzg.okrx.RxSingleHelper
import com.lzg.okrx.adapter.FlowableBody
import com.lzy.okgo.OkGo
import io.reactivex.Flowable

/**
 * 项目名称：Smart_Business
 * 创建人：小卷毛
 * 创建时间：2019-09-23 11:17
 */
class DailyViewModel : BaseViewModel() {

    fun getDailyList(
        page: Int,
        userId: String
    ): Flowable<ArrayList<CommonData>> =
        OkGo.post<BaseResponse<CommonModel>>(BaseUrl.diaryList)
            .params(getParams("userId" to userId, "page" to page))
            .converter(object : JacksonConvert<BaseResponse<CommonModel>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data.list }

    fun getAddData(vararg params: Pair<String, Any>) =
        RxSingleHelper.requestString(
            url = BaseUrl.addDiary,
            isMultipart = true,
            params = getParams(*params)
        )
            .compose(applySchedulers())
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

}